package org.example;

public class SingletonPattern {
    // 私有静态实例，确保全局唯一性
    private static SingletonPattern instance;

    // 私有化构造函数，防止外部实例化
    private SingletonPattern() {}

    // 提供全局访问点
    public static SingletonPattern getInstance() {
        if (instance == null) {
            instance = new SingletonPattern();
        }
        return instance;
    }

    public void showMessage() {
        System.out.println("Hello from Singleton!");
    }

    public static void main(String[] args) {
        SingletonPattern s1 = SingletonPattern.getInstance();
        SingletonPattern s2 = SingletonPattern.getInstance();
        System.out.println(s1 == s2); // 输出 true，表明两者是同一个实例
        s1.showMessage(); // 输出 "Hello from Singleton!"
    }
}